from django.utils.translation import ugettext_lazy as _
from oscar.apps.dashboard.catalogue.views import ProductListView as SuperProductListView

class ProductListView(SuperProductListView):
    def get_queryset(self):
        """
        Build the queryset for this list and also update the title that
        describes the queryset
        """
        description_ctx = {'upc_filter': '',
                           'title_filter': ''}
        queryset = self.model.objects.all().prefetch_related(
            'product_class', 'stockrecord__partner').order_by('title')
        #queryset = self.model.objects.all().prefetch_related(
            #'product_class', 'stockrecord__partner')\
                #.filter(attribute_values__attribute__name__contains='talle')\
                #.order_by('title', 'attribute_values__value_text')
        self.form = self.form_class(self.request.GET)
        if not self.form.is_valid():
            self.description = self.description_template % description_ctx
            return queryset

        data = self.form.cleaned_data

        if data['upc']:
            queryset = queryset.filter(upc=data['upc'])
            description_ctx['upc_filter'] = _(" including an item with UPC '%s'") % data['upc']

        if data['title']:
            queryset = queryset.filter(title__icontains=data['title']).distinct()
            description_ctx['title_filter'] = _(" including an item with title matching '%s'") % data['title']

        self.description = self.description_template % description_ctx
        return queryset
